<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 志愿服务管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <style>
        .login-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, var(--primary-light) 0%, var(--primary-color) 100%);
        }

        .login-card {
            background: var(--background-white);
            border-radius: var(--border-radius-lg);
            box-shadow: var(--shadow-lg);
            width: 100%;
            max-width: 400px;
            padding: var(--spacing-xl);
        }

        .login-header {
            text-align: center;
            margin-bottom: var(--spacing-xl);
        }

        .login-header h1 {
            color: var(--primary-color);
            font-size: 2em;
            margin-bottom: var(--spacing-md);
        }

        .login-header p {
            color: var(--text-secondary);
        }

        .login-form .form-group {
            margin-bottom: var(--spacing-lg);
        }

        .login-form .form-control {
            padding: var(--spacing-md);
            font-size: 1.1em;
        }

        .login-form .input-group-text {
            background-color: transparent;
            border-right: none;
        }

        .login-form .form-control {
            border-left: none;
        }

        .login-form .input-group:focus-within {
            box-shadow: 0 0 0 2px var(--primary-light);
            border-radius: var(--border-radius-sm);
        }

        .btn-login {
            width: 100%;
            padding: var(--spacing-md);
            font-size: 1.1em;
            margin-top: var(--spacing-lg);
        }

        .login-footer {
            text-align: center;
            margin-top: var(--spacing-xl);
        }

        .login-footer a {
            color: var(--primary-color);
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .login-footer a:hover {
            color: var(--primary-dark);
        }

        .role-selector {
            display: flex;
            gap: var(--spacing-md);
            margin-bottom: var(--spacing-lg);
        }

        .role-option {
            flex: 1;
            text-align: center;
            padding: var(--spacing-md);
            border: 2px solid var(--border-color);
            border-radius: var(--border-radius-md);
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .role-option.active {
            border-color: var(--primary-color);
            background-color: var(--primary-light);
            color: var(--primary-dark);
        }

        .role-option:hover {
            border-color: var(--primary-color);
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-card fade-in">
            <div class="login-header">
                <h1>志愿服务管理系统</h1>
                <p>欢迎回来！请登录您的账号</p>
            </div>

            <form class="login-form" id="loginForm" method="post">
                <div class="role-selector">
                    <div class="role-option active" data-role="user">
                        <i class="fas fa-user mb-2"></i>
                        <div>用户登录</div>
                    </div>
                    <div class="role-option" data-role="admin">
                        <i class="fas fa-user-shield mb-2"></i>
                        <div>管理员登录</div>
                    </div>
                </div>

                <input type="hidden" name="t1" value="user" id="roleInput">

                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-text">
                            <i class="fas fa-user"></i>
                        </span>
                        <input type="text" class="form-control" name="l_mobile" id="l_mobile" placeholder="手机号" required>
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <span class="input-group-text">
                            <i class="fas fa-lock"></i>
                        </span>
                        <input type="password" class="form-control" name="l_password" id="l_password" placeholder="密码" required>
                    </div>
                </div>

                <p id="msg" th:text="${msg}" th:style="'color: red;'" class="text-center mb-3"></p>

                <button type="submit" class="btn btn-primary btn-login">
                    <i class="fas fa-sign-in-alt me-2"></i>登录
                </button>

                <div class="login-footer">
                    <p>还没有账号？ 
                        <a href="#" id="registerLink">立即注册</a>
                    </p>
                    <div th:if="${param.error}" class="alert alert-danger mt-3">
                        用户名或密码错误
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script>
        document.querySelectorAll('.role-option').forEach(option => {
            option.addEventListener('click', function() {
                // Remove active class from all options
                document.querySelectorAll('.role-option').forEach(opt => {
                    opt.classList.remove('active');
                });
                
                // Add active class to clicked option
                this.classList.add('active');
                
                // Update hidden input value
                const role = this.dataset.role;
                document.getElementById('roleInput').value = role;
                
                // Update register link based on role
                const registerLink = document.getElementById('registerLink');
                if (role === 'admin') {
                    registerLink.href = '/toAdminRegister';
                } else {
                    registerLink.href = '/toRegister';
                }
            });
        });

        // Initialize register link
        document.getElementById('registerLink').href = '/toRegister';

        // Form submission
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault();
            
            const mobile = document.getElementById('l_mobile').value;
            const password = document.getElementById('l_password').value;
            const role = document.getElementById('roleInput').value;

            if (!mobile) {
                alert('请输入手机号');
                return false;
            }

            if (!password) {
                alert('请输入密码');
                return false;
            }

            // 验证手机号格式
            const mobilePattern = /^1[3-9]\d{9}$/;
            if (!mobilePattern.test(mobile)) {
                alert('请输入正确的手机号格式');
                return false;
            }

            // 根据角色设置提交地址
            this.action = role === 'user' ? '/toUser' : '/toAdmin';
            
            // 提交表单
            this.submit();
        });

        // 错误消息自动消失
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(function() {
                const msg = document.getElementById('msg');
                if (msg) {
                    msg.style.display = 'none';
                }
            }, 3000);
        });
    </script>
</body>
</html>